package com.example.dormitory_manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dormitory_manager.entity.DormitoryAdmin;
import com.example.dormitory_manager.form.RuleForm;
import com.example.dormitory_manager.form.SearchForm;
import com.example.dormitory_manager.mapper.DormitoryAdminMapper;
import com.example.dormitory_manager.service.DormitoryAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dormitory_manager.vo.PageVo;
import com.example.dormitory_manager.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-10-02
 */

/**
 *
 */


@Service
public class DormitoryAdminServiceImpl extends ServiceImpl<DormitoryAdminMapper, DormitoryAdmin> implements DormitoryAdminService {

    @Autowired
    private DormitoryAdminMapper dormitoryAdminMapper;


    @Override
    public ResultVo login(RuleForm ruleForm) {
        //判断用户名是否存在
        QueryWrapper<DormitoryAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", ruleForm.getUsername());
        DormitoryAdmin dormitoryAdmin = this.dormitoryAdminMapper.selectOne(queryWrapper);
        ResultVo resultVo = new ResultVo();
        if (dormitoryAdmin==null) {
            resultVo.setCode(-1);
        } else {
            //判断密码是否正确
            if (!dormitoryAdmin.getPassword().equals(ruleForm.getPassword())) {
                resultVo.setCode(-2);
            } else {
                resultVo.setCode(0);
                resultVo.setData(dormitoryAdmin);
            }
        }
        return resultVo;
    }

    @Override
    public PageVo list(Integer page, Integer size) {
        Page<DormitoryAdmin> dormitoryAdminPage =new Page<>(page,size);
        Page<DormitoryAdmin> resultPage=this.dormitoryAdminMapper.selectPage(dormitoryAdminPage,null);
        PageVo pageVo=new PageVo();
        pageVo.setData(resultPage.getRecords());
        pageVo.setTotal(resultPage.getTotal());
        return pageVo;
    }

    @Override
    public PageVo search(SearchForm searchForm) {
        //模糊查询+分页
        Page<DormitoryAdmin> dormitoryAdminPage=new Page<>(searchForm.getPage(),searchForm.getSize());
        Page<DormitoryAdmin> resultPage=null;
//      //判断要不要模糊查询
        if(searchForm.getValue().equals("")){
            resultPage=this.dormitoryAdminMapper.selectPage(dormitoryAdminPage,null);
        }else{
            QueryWrapper<DormitoryAdmin> queryWrapper=new QueryWrapper<>();
            queryWrapper.like(searchForm.getKey(),searchForm.getValue());
            resultPage=this.dormitoryAdminMapper.selectPage(dormitoryAdminPage,queryWrapper);
        }
        PageVo pageVo=new PageVo();
        pageVo.setData(resultPage.getRecords());
        pageVo.setTotal(resultPage.getTotal());
        return pageVo;
    }
}
